package com.graduate.cec.service;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import com.graduate.cec.common.ResponseCode;
import com.graduate.cec.exception.CecServiceException;
import com.graduate.cec.mapper.NationDetailMapper;
import com.graduate.cec.pojo.dto.NationDetailDTO;
import com.graduate.cec.pojo.entity.NationDetail;
import com.graduate.cec.pojo.entity.NationDetailExample;
import com.graduate.cec.pojo.vo.NationDetailVO;
import com.graduate.cec.util.CopyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class NationDetailService {

    @Resource
    private NationDetailMapper nationDetailMapper;


    String dirPath = null;

    /****************        增       ******************************************************************/
    public NationDetailVO insertNationDetail(NationDetailDTO nationDetailDTO){
        NationDetail nationDetail = CopyUtil.copy(nationDetailDTO,NationDetail.class);
        int i = nationDetailMapper.insertSelective(nationDetail);
        if (i == 1){
            NationDetailVO copy = CopyUtil.copy(nationDetail, NationDetailVO.class);
            return copy;
        }else {
            throw new CecServiceException(ResponseCode.INSERT_INFO_ERROR,"新增失败!");
        }

    }

    //上传图片
    public String uploadPictures(MultipartFile picFile) throws Exception {
        //得到保存文章图片文件夹的相对路径
        //getURL(File file)
        //获得URL，常用于使用绝对路径时的情况
        //file(String path)
        //创建File对象，自动识别相对或绝对路径，相对路径将自动从ClassPath下寻找
        String urlStr = URLUtil.getURL(FileUtil.file("picture")).toString();
        log.info("urlStr = :{}",urlStr);
        //urlStr = :file:/D:/runtimeSoftware/IDEA/graduation/kbs/target/classes/articlePictures
        dirPath = urlStr.substring(urlStr.indexOf("cec"), urlStr.indexOf("target"));
        //dirPath = kbs/
        log.info("dirPath = " + dirPath);
        //prefix = D:/Danei/
        String prefix = urlStr.substring(6, urlStr.indexOf("cec"));
        log.info("prefix = " + prefix);
        dirPath += "picture/";
        dirPath = prefix + dirPath;
        log.info("dirPath = " + dirPath);
        //dirPath = D:/Danei/ushare-management/articlePictures/
        log.info("picFile = " + picFile);
        //得到原始文件名
        String fileName = picFile.getOriginalFilename();

        log.info("fileName = " + fileName);
        //得到后缀名 从最后一个.出现的位置截取到最后
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //得到唯一文件名 UUID.randomUUID()得到一个唯一标识符
        fileName = UUID.randomUUID() + suffix;
        log.info("fileName = " + fileName);
        //准备保存图片的文件夹路径
        File dirFile = new File(dirPath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        //得到文件的完整路径
        String filePath = dirPath + fileName;
        log.info("filePath = " + filePath);
        //把文件保存到此路径
        picFile.transferTo(new File(filePath));
        return "http://localhost:19090/picture/" + fileName;
    }








    /****************        删       ******************************************************************/
    public void deleteByKeys(Long nationId){
        NationDetailExample nationDetailExample = new NationDetailExample();
        NationDetailExample.Criteria criteria = nationDetailExample.createCriteria();
        criteria.andNationIdEqualTo(nationId);
        int i = nationDetailMapper.deleteByExample(nationDetailExample);
        if (i > 0){
            return ;
        }else{
            throw new CecServiceException(ResponseCode.DELETE_INFO_ERROR,"删除失败!");
        }
    }

    //删除上传的图片
    public void removePicture(String pictureName) {
        String filePath = dirPath + "/" + pictureName;
        new File(filePath).delete();
    }




    /****************        改       ******************************************************************/
    public void updateByKey(NationDetailDTO nationDetailDTO){
        NationDetail nationDetail = CopyUtil.copy(nationDetailDTO, NationDetail.class);
        int i = nationDetailMapper.updateByPrimaryKeySelective(nationDetail);
        if (i == 1){
            return;
        }else{
            throw new CecServiceException(ResponseCode.UPDATE_INFO_ERROR,"修改失败");
        }
    }

    /****************        查       ******************************************************************/
    public NationDetailVO getListByExample(Long nationId){
        NationDetailExample nationDetailExample = new NationDetailExample();
        NationDetailExample.Criteria criteria = nationDetailExample.createCriteria();
        criteria.andNationIdEqualTo(nationId);
        List<NationDetail> nationDetails = nationDetailMapper.selectByExampleWithBLOBs(nationDetailExample);
        //log.info("nationDetails:{}",nationDetails.get(0));
        return CopyUtil.copy(nationDetails.get(0), NationDetailVO.class);
    }
}